/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Folder.FolderOptions;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CreateFolder
/*     */   extends MailDocumentHandler
/*     */ {
/*  42 */   private static final String[] TARGET_FOLDER_PATH = { "folder", "l" };
/*  43 */   private static final String[] RESPONSE_ITEM_PATH = new String[0];
/*  44 */   protected String[] getProxiedIdPath(Element request) { return TARGET_FOLDER_PATH; }
/*  45 */   protected boolean checkMountpointProxy(Element request) { return true; }
/*  46 */   protected String[] getResponseItemPath() { return RESPONSE_ITEM_PATH; }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  50 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  51 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  52 */     OperationContext octxt = getOperationContext(zsc, context);
/*  53 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*  55 */     Element t = request.getElement("folder");
/*  56 */     String name = t.getAttribute("name");
/*  57 */     String view = t.getAttribute("view", null);
/*  58 */     String flags = t.getAttribute("f", null);
/*  59 */     byte color = (byte)(int)t.getAttributeLong("color", 0L);
/*  60 */     String rgb = t.getAttribute("rgb", null);
/*  61 */     String url = t.getAttribute("url", null);
/*  62 */     String folderId = t.getAttribute("l", null);
/*  63 */     ItemId iidParent = folderId != null ? new ItemId(folderId, zsc) : null;
/*  64 */     boolean fetchIfExists = t.getAttributeBool("fie", false);
/*  65 */     boolean syncToUrl = t.getAttributeBool("sync", true);
/*  66 */     ACL acl = FolderAction.parseACL(t.getOptionalElement("acl"), MailItem.Type.of(view), mbox.getAccount());
/*     */     
/*     */ 
/*     */ 
/*  70 */     boolean alreadyExisted = false;
/*     */     try
/*     */     {
/*  73 */       Folder.FolderOptions fopt = new Folder.FolderOptions();
/*  74 */       fopt.setColor(rgb != null ? new Color(rgb) : new Color(color)).setUrl(url);
/*  75 */       fopt.setDefaultView(MailItem.Type.of(view)).setFlags(Flag.toBitmask(flags));
/*  76 */       Folder folder; if (iidParent != null) {
/*  77 */         folder = mbox.createFolder(octxt, name, iidParent.getId(), fopt);
/*     */       } else {
/*  79 */         folder = mbox.createFolder(octxt, name, fopt);
/*     */       }
/*     */       
/*  82 */       if ((!folder.getUrl().equals("")) && (syncToUrl))
/*     */         try {
/*  84 */           mbox.synchronizeFolder(octxt, folder.getId());
/*     */         }
/*     */         catch (ServiceException e) {
/*  87 */           rollbackFolder(folder);
/*  88 */           throw e;
/*     */         }
/*     */         catch (RuntimeException e) {
/*  91 */           rollbackFolder(folder);
/*  92 */           throw ServiceException.FAILURE("could not synchronize with remote feed", e);
/*     */         }
/*     */     } catch (ServiceException se) {
/*     */       Folder folder;
/*  96 */       if ((se.getCode() == "mail.ALREADY_EXISTS") && (fetchIfExists)) { Folder folder;
/*  97 */         if (iidParent != null) {
/*  98 */           folder = mbox.getFolderByName(octxt, iidParent.getId(), name);
/*     */         } else {
/* 100 */           folder = mbox.getFolderByPath(octxt, name);
/*     */         }
/* 102 */         alreadyExisted = true;
/*     */       } else {
/* 104 */         throw se;
/*     */       }
/*     */     }
/*     */     
/*     */     Folder folder;
/* 109 */     if ((acl != null) && (!alreadyExisted)) {
/*     */       try {
/* 111 */         mbox.setPermissions(octxt, folder.getId(), acl);
/*     */       }
/*     */       catch (ServiceException e) {
/*     */         try {
/* 115 */           mbox.delete(null, folder.getId(), MailItem.Type.FOLDER);
/*     */         } catch (ServiceException nse) {
/* 117 */           ZimbraLog.soap.warn("error ignored while rolling back folder create", nse);
/*     */         }
/* 119 */         throw e;
/*     */       }
/*     */     }
/*     */     
/* 123 */     Element response = zsc.createElement(MailConstants.CREATE_FOLDER_RESPONSE);
/* 124 */     if (folder != null)
/* 125 */       ToXML.encodeFolder(response, ifmt, octxt, folder);
/* 126 */     return response;
/*     */   }
/*     */   
/*     */   private void rollbackFolder(Folder folder) {
/*     */     try {
/* 131 */       folder.getMailbox().delete(null, folder.getId(), MailItem.Type.FOLDER);
/*     */     } catch (ServiceException nse) {
/* 133 */       ZimbraLog.mailbox.warn("error ignored while rolling back folder create", nse);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CreateFolder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */